<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");

class YzController extends Controller {
	public function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}

	//用户注册
	public function zhuce_add(){
		$user = I("user");
		$data["LogonPass"] = I("pass");//密码
		$data["yq_code"] = I("yaoqingma");//邀请码

		$yaoqing = I("yaoqingma");

		$agent = M("agent")->where("code = {$yaoqing}")->field("type")->find();

		foreach($agent as $v){
			$types[] = $v["type"];
		}
		$type = implode(",",$types);

		$frist = substr( $type, 0, 1 );

		$data["utype"] = $frist;//utype 1代理 2玩家
		$data["avatar"] = "/style/images/pimg17.jpg"; //默认给张图片
		$yanzhengma = I("yanzhengma");
		$yaoqingma = I("yaoqingma");


       //检查验证码是否正确
		if(!self::check_verify($yanzhengma)){
			echo 0;die;
		}
		$date = M('Accountsinfo')->field("accounts")->select();
		if($yaoqingma){
			$u = M('agent')->where("code in($yaoqingma)")->find();
		}
		foreach($date as $v){
			$accounts[] = $v['accounts'];
		}

		if(in_array($user,$accounts)){
			echo 1;die;
		}elseif($yaoqingma != $u['code']){
			echo 2;die;
		}else{
			$data["Accounts"] = $user;
			$data['RegAccounts'] = $user;
			$data['LastLogonIP'] = 0;
			$data['RegisterIP'] = 0;
		}

		$uid = M('Accountsinfo')->add($data);

//添加到数据库
	//up_level 表
		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 1;
        M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 2;
		M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 3;
		M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 4;
		M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 5;
		M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 6;
		M("up_level")->add($dat);

		$dat['uid'] = $uid;
		$dat['level'] = 1;
		$dat['ctime'] = time();
		$dat['kl_money'] = 0;
		$dat['is_lingqu'] = 0;
		$dat['lq_time'] = 0;
		$dat['type'] = 7;
		M("up_level")->add($dat);

		echo 3;
	}

	//忘记密码 验证
	public function resetpwd(){
		$user = I("user");
		$yanzhengma = I("yzm");

		$date = M('Accountsinfo')->field("accounts")->select();

		foreach($date as $v){
			$accounts[] = $v['accounts'];
		}
		$users = in_array($user,$accounts);
		if(empty($users)){
			echo 1;die;
		}elseif(!self::check_verify($yanzhengma)){
			echo 2;die;
		}else{
			echo 3;
		}
	}



}